package com.dhcc.android.base.common.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dhcc.android.base.R;
import com.dhcc.android.base.common.bean.KeyValueBean;
import com.dhcc.android.base.common.view.AutoCompleteTextView.InitAutoCompleteUtil;
import com.dhcc.android.base.base.MSApplication;

import java.util.List;

/**
 * 此为Group为两栏时，Adapter的记忆控制。
 * 
 * @author pengling
 * 
 */
public class ListViewPoupAdapter extends BaseAdapter {
	private Context context;
	private List<KeyValueBean> list;
	private boolean isDelete;

	public ListViewPoupAdapter(Context context, List<KeyValueBean> list,boolean isDelete) {
		this.context = context;
		this.list = list;
		this.isDelete=isDelete;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("static-access")
	@Override
	public View getView(final int position, View convertView, ViewGroup viewGroup) {
		ViewHolder holder;
		// TextView groupItem;
		ImageView selectImage;
		KeyValueBean keyValueBean = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.util_group_item_view, null);
			holder = new ViewHolder();
			convertView.setTag(holder);
			holder.groupItem = (TextView) convertView
					.findViewById(R.id.groupItem);
			holder.groupLayout = (LinearLayout) convertView
					.findViewById(R.id.layout);
			holder.id = (TextView) convertView.findViewById(R.id.id);
			holder.delete_layout = (LinearLayout) convertView.findViewById(R.id.delete_layout);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		selectImage = (ImageView) holder.groupLayout
				.findViewById(R.id.item_select);
		holder.groupItem.setText(keyValueBean.getValue());
		holder.id.setText(keyValueBean.getKey());
//		selectImage.setVisibility(View.GONE);
//		holder.groupLayout.setGravity(Gravity.CENTER);
		if(isDelete){
			holder.delete_layout.setVisibility(View.VISIBLE);
		}else{
			holder.delete_layout.setVisibility(View.GONE);
		}
		holder.delete_layout.setOnClickListener(new OnClickListener() {//还需封装
			@Override
			public void onClick(View v) {
				KeyValueBean obj = list.remove(position);
				//网关配置
				InitAutoCompleteUtil.deleteHistory(context, MSApplication.CONFIG_HISTORY, obj.getValue());
				list.remove(obj);
				notifyDataSetChanged();
			}
		});

		return convertView;
	}

	static class ViewHolder {
		TextView groupItem;
		TextView id;
		LinearLayout groupLayout;
		LinearLayout delete_layout;
	}

}
